<template>
    <div>
        <el-dialog
                width="450px"
                title="修改商品"
                :visible.sync="updateShow"
                :show-close="false"
                :before-close="handleClose"
                @open="onOpen">
            <el-form label-position="right" :rules="rules" :model="updateGood" ref="ruleForm">
                <el-form-item label="商品图片">
                    <el-upload
                            :multiple="false"
                            ref="upload"
                            class="upload-demo"
                            action="#"
                            :auto-upload="false"
                            :file-list="HFfileList"
                            :before-upload="beforeUpload"
                            list-type="picture-card"
                            :on-preview="handlePictureCardPreview"
                            :on-remove="HFhandleRemove"
                            :on-change="HFhandleChangeImg">
                        <i class="el-icon-plus"></i>
                        <div slot="tip" class="el-upload__tip">
                            只能上传jpg/png文件，且不超过5Mb
                        </div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="商品名称" prop="name" label-width="80px">
                    <el-input v-model=" updateGood.name"/>
                </el-form-item>
                <el-form-item label="库存数量" label-width="80px">
                    <el-input-number v-model="updateGood.stock" :min="0"
                                     @blur="handleChange"></el-input-number>
                </el-form-item>
                <el-form-item label="所需积分" label-width="80px">
                    <el-input-number v-model="updateGood.exchange" :min="1"
                                     @blur="handleChange"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submit">提 交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import upload from '@/utils/methods/upload'
    import {updateExchangeGoodInfo} from '@/utils/apis/integral'

    export default {
        name: "UpdateExchange",
        props: ['updateShow', 'updateGood'],
        data() {
            return {
                rules: {
                    name: [
                        {required: true, message: '请输入商品名称', trigger: 'blur'},
                        {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                    ]
                },
                HFfileList: []
            }
        },
        methods: {
            ...upload,
            //打开窗口回调
            onOpen() {
            },
            handleClose() {
                this.$message.info('取消修改')
                this.$emit('cancel')
            },
            //防止数量为''
            handleChange() {
                if (this.updateGood.stock === undefined) {
                    this.updateGood.stock = 1
                }
                if (this.updateGood.exchange === undefined) {
                    this.updateGood.exchange = 1
                }
            },
            //提交
            submit() {
                console.log(this.updateGood)
                this.$refs['ruleForm'].validate(async valid => {
                    if (valid) {
                        const data = {
                            integralGoodsStr: JSON.stringify(this.updateGood),
                            file: this.file
                        }
                        await updateExchangeGoodInfo(data).then(() => {
                            this.$message.success('修改成功')
                            this.$emit('cancel')
                            this.$emit('updateSuccess')
                            this.HFfileList=[]
                        })
                    } else {
                        this.$message.warning('表单格式不正确')
                    }
                })
            }
        },
    }
</script>

<style scoped>

</style>